home *** CD-ROM | disk | FTP | other *** search
/ Hottest 6 / Hottest 6 (1996)(PDSoft)[!].iso / software / rendering / real3d / shelly / shelly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-11-24  |  13.1 KB  |  613 lines

  1. /*****************************************************/
  2. /* Shelly: The ShellShapeGenerator by:               */
  3. /*         RANDi                                     */
  4. /*              (rschultz@informatik.uni-rostock.de) */
  5. /*****************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <memory.h>
  12.  
  13. #include "shelly.h"
  14.  
  15. int countw=0, counth=0;
  16.  
  17. /********************************************/
  18. /*cot:                                      */
  19. /********************************************/
  20. double cot(double in)
  21. {
  22.  if (sin(in) == 0) 
  23.   return(0);
  24.  else 
  25.   return(cos(in)/sin(in));
  26. }
  27.  
  28.  
  29. /********************************************/
  30. /*round:                                    */
  31. /********************************************/
  32. double round(double in)
  33. {
  34.  if((floor(in)-in) < 0)
  35.   return(floor(in));
  36.  else
  37.   return(floor(in)+1);
  38. }
  39.  
  40. /********************************************/
  41. /*myfree:                                   */
  42. /********************************************/
  43. int myfree(struct punkt *anker1)
  44. {
  45.  struct punkt *hp, *hpnew;
  46.  if (anker1 == NULL)
  47.   return(0);
  48.  
  49.  hp = anker1;
  50.  hpnew = hp;
  51.  do
  52.  {
  53.   hp = hpnew;
  54.   hpnew = (*hp).next;
  55.  
  56.   cfree(hp);
  57.  }
  58.  while(hpnew);
  59.  
  60. return(0);
  61. }
  62.  
  63.  
  64. /********************************************/
  65. /*mycopystr:                                */
  66. /********************************************/
  67. char *mycopystr(char *source,char *destination,char endchar)
  68. {
  69.  int ende = 1;
  70.  char *merk;
  71.  
  72.  merk = destination;        /* merk points now on destination */
  73.  while (ende)
  74.  {
  75.   if ((*source == '\0') || (*source == endchar)) 
  76.   {
  77.   ende = 0;
  78.   }
  79.   else
  80.   {
  81.    *destination = *source; 
  82.    destination++;
  83.    source++;
  84.   } /* if */
  85.  } /* while */
  86.  *destination = '\0';
  87. return(merk);
  88. }
  89.  
  90.  
  91. /********************************************/
  92. /*ReadInfile:                               */
  93. /********************************************/
  94.  
  95. void ReadInfile(struct ShellyArguments *ShellyArgs,char *fin)
  96. {
  97.  char readline[laenge], hilf[laenge], *hp;
  98.  FILE *fp;
  99.  
  100.  (*ShellyArgs).output = POV;
  101.  
  102.  fp = fopen(fin,"r");
  103.  if (fp != NULL) 
  104.  {
  105.   fprintf(stderr,"Parsing:");
  106.   while (fgets(readline,laenge,fp))
  107.   {
  108.    fprintf(stderr,".");
  109.  
  110.    hp = strstr(readline,"alpha:");
  111.    if (hp != NULL)
  112.    {
  113.     mycopystr(hp+6,hilf,'\0');
  114.     (*ShellyArgs).alpha = atof(hilf);
  115.    } /* if */
  116.  
  117.    hp = strstr(readline,"beta:");
  118.    if (hp != NULL)
  119.    {
  120.     mycopystr(hp+5,hilf,'\0');
  121.     (*ShellyArgs).beta = atof(hilf);
  122.    } /* if */
  123.    hp = strstr(readline,"phi:");
  124.    if (hp != NULL)
  125.    {
  126.     mycopystr(hp+4,hilf,'\0');
  127.     (*ShellyArgs).phi = atof(hilf);
  128.    } /* if */
  129.  
  130.    hp = strstr(readline,"my:");
  131.    if (hp != NULL)
  132.    {
  133.     mycopystr(hp+3,hilf,'\0');
  134.     (*ShellyArgs).my = atof(hilf);
  135.    } /* if */
  136.  
  137.    hp = strstr(readline,"omega:");
  138.    if (hp != NULL)
  139.    {
  140.     mycopystr(hp+6,hilf,'\0');
  141.     (*ShellyArgs).omega = atof(hilf);
  142.    } /* if */
  143.  
  144.    hp = strstr(readline,"smin:");
  145.    if (hp != NULL)
  146.    {
  147.     mycopystr(hp+5,hilf,'\0');
  148.     (*ShellyArgs).smin = atof(hilf);
  149.    } /* if */
  150.   
  151.    hp = strstr(readline,"smax:");
  152.    if (hp != NULL)
  153.    {
  154.     mycopystr(hp+5,hilf,'\0');
  155.     (*ShellyArgs).smax = atof(hilf);
  156.    } /* if */
  157.  
  158.    hp = strstr(readline,"sd:");
  159.    if (hp != NULL)
  160.    {
  161.     mycopystr(hp+3,hilf,'\0');
  162.     (*ShellyArgs).sd = atof(hilf);
  163.    } /* if */
  164.  
  165.    hp = strstr(readline,"A:");
  166.    if (hp != NULL)
  167.    {
  168.     mycopystr(hp+2,hilf,'\0');
  169.     (*ShellyArgs).A = atof(hilf);
  170.    } /* if */
  171.  
  172.    hp = strstr(readline,"a:");
  173.    if (hp != NULL)
  174.    {
  175.     mycopystr(hp+2,hilf,'\0');
  176.     (*ShellyArgs).a = atof(hilf);
  177.    } /* if */
  178.  
  179.  
  180.    hp = strstr(readline,"b:");
  181.    if (hp != NULL)
  182.    {
  183.     mycopystr(hp+2,hilf,'\0');
  184.     (*ShellyArgs).b = atof(hilf);
  185.    } /* if */
  186.  
  187.    hp = strstr(readline,"P:");
  188.    if (hp != NULL)
  189.    {
  190.     mycopystr(hp+2,hilf,'\0');
  191.     (*ShellyArgs).P = atof(hilf);
  192.    } /* if */
  193.  
  194.    hp = strstr(readline,"W1:");
  195.    if (hp != NULL)
  196.    {
  197.     mycopystr(hp+3,hilf,'\0');
  198.     (*ShellyArgs).W1 = atof(hilf);
  199.    } /* if */
  200.  
  201.    hp = strstr(readline,"W2:");
  202.    if (hp != NULL)
  203.    {
  204.     mycopystr(hp+3,hilf,'\0');
  205.     (*ShellyArgs).W2 = atof(hilf);
  206.    } /* if */
  207.  
  208.    hp = strstr(readline,"N:");
  209.    if (hp != NULL)
  210.    {
  211.     mycopystr(hp+2,hilf,'\0');
  212.     (*ShellyArgs).N = atof(hilf);
  213.    } /* if */
  214.  
  215.    hp = strstr(readline,"L:");
  216.    if (hp != NULL)
  217.    {
  218.     mycopystr(hp+2,hilf,'\0');
  219.     (*ShellyArgs).L = atof(hilf);
  220.    } /* if */
  221.  
  222.    hp = strstr(readline,"omin:");
  223.    if (hp != NULL)
  224.    {
  225.     mycopystr(hp+5,hilf,'\0');
  226.     (*ShellyArgs).omin = atof(hilf);
  227.    } /* if */
  228.  
  229.    hp = strstr(readline,"omax:");
  230.    if (hp != NULL)
  231.    {
  232.     mycopystr(hp+5,hilf,'\0');
  233.     (*ShellyArgs).omax = atof(hilf);
  234.    } /* if */
  235.  
  236.    hp = strstr(readline,"od:");
  237.    if (hp != NULL)
  238.    {
  239.     mycopystr(hp+3,hilf,'\0');
  240.     (*ShellyArgs).od = atof(hilf);
  241.    } /* if */
  242.  
  243.    hp = strstr(readline,"POV");
  244.    if (hp != NULL)
  245.    {
  246.     (*ShellyArgs).output = POV;
  247.    } /* if */
  248.  
  249.    hp = strstr(readline,"RPL");
  250.    if (hp != NULL)
  251.    {
  252.     (*ShellyArgs).output = RPL;
  253.    } /* if */
  254.  
  255.   } /* while */
  256.  
  257.   fprintf(stderr,"ready!\n");
  258.   if (fclose(fp) != 0) 
  259.   fprintf(stderr,"Error while closing file: %s!\n",fin);
  260.  }
  261.  else
  262.  {
  263.   fprintf(stderr,"Could not open datafile: %s!\n",fin);
  264.   exit(5);
  265.  }
  266. } /* ReadInfile */
  267.  
  268.  
  269. /********************************************/
  270. /* writepovtriangles:                       */
  271. /********************************************/
  272. int writepovtriangles(FILE *fp,struct punkt *anker1,struct punkt *anker2)
  273. {
  274.  struct punkt *p11, *p12, *p21, *p22;
  275.  
  276.  if ((anker1 == NULL) || (anker2 == NULL))
  277.   return(0);
  278.  
  279.  p11 = anker1;
  280.  p21 = anker2;
  281.  p12 = p11;
  282.  p22 = p21;
  283.  
  284.  do
  285.  {
  286.   p11 = p12;
  287.   p21 = p22;
  288.   p12 = (*p11).next;
  289.   p22 = (*p21).next;
  290.  
  291.   if ((p12 != NULL) && (p22 != NULL))
  292.   {
  293.    fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p11).x,(*p11).y,(*p11).z,(*p12).x,(*p12).y,(*p12).z,(*p21).x,(*p21).y,(*p21).z);
  294.    fprintf(fp,"pigment{te}}\n");
  295.  
  296.    fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p21).x,(*p21).y,(*p21).z,(*p12).x,(*p12).y,(*p12).z,(*p22).x,(*p22).y,(*p22).z);
  297.    fprintf(fp,"pigment{te}}\n");
  298.   }
  299.  }
  300.  while((p12 != NULL) && (p22 != NULL));
  301.  
  302.  return(0);
  303. } /* writepovtriangles */
  304.  
  305.  
  306. /********************************************/
  307. /* writerplline:                            */
  308. /********************************************/
  309. int writerplline(FILE *fp,struct punkt *anker1)
  310. {
  311.  struct punkt *p11, *p12;
  312.  
  313.  if (anker1 == NULL)
  314.   return(0);
  315.  
  316.  p11 = anker1;
  317.  p12 = p11;
  318.  
  319.  countw++;                                /* calc number of lines created */
  320.  
  321.  fprintf(fp,"%.2f  %.2f  %.2f\n",(*p11).x,(*p11).y,(*p11).z);
  322.  
  323.  do
  324.  {
  325.   p11 = p12;
  326.   p12 = (*p11).next;
  327.  
  328.   if (p12 != NULL)
  329.   {
  330.    fprintf(fp,"%.2f  %.2f  %.2f\n",(*p12).x,(*p12).y,(*p12).z);   
  331.   }
  332.  }
  333.  while(p12 != NULL);
  334.  
  335.  return(0);
  336. } /* writerplline */
  337.  
  338.  
  339.  
  340. /********************************************/
  341. /* writepovheader: reads in camera/light's  */
  342. /*                                          */
  343. /********************************************/
  344.  
  345. void writepovheader(FILE *fp)
  346. {
  347.  FILE *fp2;
  348.  int rf;
  349.  
  350.  fp2 = fopen("povheader.pov","r");
  351.  
  352.  if (fp2 == NULL)
  353.  {
  354.   fprintf(stderr,"Cannot open File: 'shelly.pov'\n");
  355.   fprintf(stderr,"Creating File with standard Camera/Light ... \n");
  356.  
  357.   fprintf(fp,"/* POV-Scenefile generated by Shelly1.0                  */\n");
  358.   fprintf(fp,"/* written by RANDi (rschultz@informatik.uni-rostock.de) */\n");
  359.  
  360.   fprintf(fp,"#declare te = pigment { color red 1 green 0 blue 0 }\n");
  361.  
  362.   fprintf(fp,"camera\n{\n location  <0, 500, 500>\n");
  363.   fprintf(fp," look_at <0, 0, 0>\n}\n");
  364.   
  365.   fprintf(fp,"object\n{\n light_source {\n");
  366.   fprintf(fp,"  <250, 500, 500> color red 1 green 1 blue 1\n }\n");
  367.   fprintf(fp,"}\n");
  368.   fprintf(fp,"object\n{\n light_source {\n");
  369.   fprintf(fp,"  <-250, 500, 500> color red 1 green 1 blue 1\n }\n");
  370.   fprintf(fp,"}\n");
  371.  
  372.  
  373.  }  
  374.  else
  375.  {
  376.   
  377.   do
  378.   {
  379.    rf = fgetc(fp2);
  380.    if (rf != -1) fputc(rf,fp);
  381.   }
  382.   while (rf != -1);
  383.  
  384.   fclose(fp2);
  385.  } /* if */
  386. } /* writepovheader */
  387.  
  388.  
  389. /********************************************/
  390. /* writerplfinish:                          */
  391. /*                                          */
  392. /********************************************/
  393.  
  394. void writerplfinish(FILE *fp)
  395. {
  396.   fprintf(fp,"%d\n",counth);           /* height width of the mesh */
  397.   fprintf(fp,"%d\n",countw);
  398.   
  399.   fprintf(fp,"3\n0\n255 255 255 0\n"); /* RGBA */
  400.   fprintf(fp,"\"Shell\"\n0\n");        /* Name */
  401.   fprintf(fp,"\"CEND\"\n");
  402.   fprintf(fp,"C_MESH DROP\n");
  403.  
  404. } /* writerplfinish */
  405.  
  406.  
  407. /**********************************************/
  408. /* RenderShell:                               */
  409. /*             SSIA :)                        */
  410. /**********************************************/
  411.  
  412. void RenderShell(struct ShellyArguments *ShellyArgs,char *fout)
  413.  
  414. {
  415.  struct punkt *List1anker, *List2anker, *L1p1, *L2p1, *L1p2, *L2p2;
  416.  
  417.  int countdown; 
  418.  double x,y,z,R,S,O;
  419.  double my, Re, P, W1, W2, N, L, g, k;
  420.  double smin, smax, sd, a, b, A, beta, phi, omega, alpha, omin, omax, od;   
  421.  FILE *fp;
  422.  
  423.  smin = (*ShellyArgs).smin*pi/180;
  424.  smax = (*ShellyArgs).smax*pi/180;
  425.  omin = (*ShellyArgs).omin*pi/180;
  426.  omax = (*ShellyArgs).omax*pi/180;
  427.  sd = (*ShellyArgs).sd*pi/180;
  428.  od = (*ShellyArgs).od*pi/180;
  429.  
  430.  alpha = (*ShellyArgs).alpha*pi/180;
  431.  beta = (*ShellyArgs).beta*pi/180;
  432.  phi = (*ShellyArgs).phi*pi/180;
  433.  omega = (*ShellyArgs).omega*pi/180;
  434.  my = (*ShellyArgs).my*pi/180;
  435.  
  436.  a = (*ShellyArgs).a;
  437.  A = (*ShellyArgs).A;
  438.  N = (*ShellyArgs).N;
  439.  W1 = (*ShellyArgs).W1*pi/180;
  440.  W2 = (*ShellyArgs).W2*pi/180;
  441.  P = (*ShellyArgs).P*pi/180;
  442.  L = (*ShellyArgs).L;
  443.  b = (*ShellyArgs).b;
  444.  
  445.  countdown = (fabs(omin)+fabs(omax))/od;
  446.  
  447.  S = smin;
  448.  O = omin; 
  449.  
  450.  fp = fopen(fout,"w");
  451.  if (fp != NULL) 
  452.  {
  453.   if ((*ShellyArgs).output != RPL)
  454.    writepovheader(fp);
  455.  
  456.   fprintf(stderr,"Calculating... be patient!\n");
  457.  
  458.   for(S = smin;(S+sd)<smax;S=S+sd)
  459.   {
  460.    if (N == 0) 
  461.     g = 0;
  462.    else
  463.     g  = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi))); 
  464.  
  465.    Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5); 
  466.  
  467.    if (N == 0)
  468.     k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
  469.    else
  470.     k  = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-(2*g/W2)*(2*g/W2));
  471.  
  472.    R = Re + k;
  473.  
  474.   /* alternatively: */
  475.   /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
  476.  
  477.    x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));   
  478.    y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
  479.    z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
  480.  
  481.    if ((L1p1 = calloc(1,sizeof(struct punkt))) == NULL)
  482.    {
  483.     fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
  484.     fclose(fp);
  485.     myfree(List2anker);
  486.     myfree(List1anker);
  487.     exit(5);
  488.    } /* if */
  489.  
  490.    if (S == smin) 
  491.     List1anker = L1p1;
  492.    else
  493.     (*L1p2).next = L1p1;
  494.    L1p2 = L1p1;
  495.  
  496.    (*L1p1).x = x;
  497.    (*L1p1).y = y;
  498.    (*L1p1).z = z;
  499.  
  500.   } /* for */
  501.  
  502.   if ((*ShellyArgs).output == RPL)
  503.    writerplline(fp,List1anker);
  504.  
  505.   for(O = omin+od;(O+od)<omax;O=O+od)
  506.   {
  507.  
  508.    fprintf(stderr,"%d\r",countdown--);             /* produce a countdown */
  509.    fflush(stderr);
  510.  
  511.    counth = 0;
  512.  
  513.    for(S = smin;(S+sd)<smax;S=S+sd)
  514.    {
  515.  
  516.     counth++;                           /* calc number of points per line */
  517.   
  518.     if (N == 0) 
  519.      g = 0;
  520.     else
  521.      g  = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi)));
  522.  
  523.     Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5); 
  524.     if (N == 0)
  525.      k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
  526.     else
  527.      k  = L*exp(-1*(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-1*(2*g/W2)*(2*g/W2));
  528.  
  529.  
  530.     R = Re + k;
  531.  
  532.     /* alternatively: */
  533.     /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
  534.  
  535.     x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));   
  536.     y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
  537.     z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
  538.  
  539.     if ((L2p1 = calloc(1,sizeof(struct punkt))) == NULL)
  540.     {
  541.      fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
  542.      fclose(fp);
  543.      myfree(List1anker);
  544.      myfree(List1anker);
  545.      exit(5);
  546.     } /* if */
  547.  
  548.     if (S == smin)
  549.      List2anker = L2p1;
  550.     else
  551.      (*L2p2).next = L2p1;
  552.     L2p2 = L2p1;
  553.  
  554.     (*L2p1).x = x;
  555.     (*L2p1).y = y;
  556.     (*L2p1).z = z;
  557.  
  558.  
  559.    } /* for */
  560.  
  561.    if ((*ShellyArgs).output != RPL)
  562.     writepovtriangles(fp,List1anker,List2anker);    
  563.    else
  564.     writerplline(fp,List2anker);
  565.  
  566.    myfree(List1anker);
  567.    List1anker = List2anker;
  568.  
  569.  
  570.   } /* for */
  571.  
  572.   if ((*ShellyArgs).output == RPL)
  573.    writerplfinish(fp);
  574.  
  575.  
  576.   fprintf(stderr,"Calculation complete ... !\n");
  577.  
  578.   myfree(List1anker);
  579.  
  580.   if (fclose(fp) != 0) 
  581.   fprintf(stderr,"Error while closing file: %s!\n",fout);
  582.  }
  583.  else
  584. fprintf(stderr,"Could not open outfile: %s!\n",fout);
  585.  
  586. } /* RenderShell */
  587.  
  588.  
  589.  
  590.  
  591. /********************************************/
  592. /*main:                                     */
  593. /********************************************/
  594. int main(int ac,char **av)
  595.  
  596. {
  597.  
  598.  struct ShellyArguments ShellyArgs;
  599.  
  600.  if (ac < 3)
  601.  {
  602.   fprintf(stderr,"Expected atleast 2 Arguments!\nUSAGE:");
  603.   fprintf(stderr,"   'shelly infilename outfilename'\n");
  604.   exit(0);
  605.  } /* if */
  606.  
  607.  ReadInfile(&ShellyArgs,av[1]); 
  608.  
  609.  RenderShell(&ShellyArgs,av[2]); 
  610.  
  611.  exit(0);
  612. }
  613.